package ui.swing.textcomponent.autocompleting;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class App extends JFrame implements ActionListener {

	private static final long serialVersionUID = -5538302868297874605L;

	CompletableJTextField completableField;
	JTextField completionField;

	public App() {
		super("自动补全文字");
		setBounds(200, 200, 300, 500);

		JPanel contentPane = new JPanel();
		contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));

		completableField = new CompletableJTextField(60);
		contentPane.add(completableField);

		JPanel bottom = new JPanel();
		bottom.add(new JLabel("Completion:"));
		completionField = new JTextField(40);
		completionField.addActionListener(this);
		bottom.add(completionField);
		JButton addButton = new JButton("Add");
		addButton.addActionListener(this);
		bottom.add(addButton);
		contentPane.add(bottom);
		
		setContentPane(contentPane);
		pack();
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		completableField.addCompletion (completionField.getText());
		completionField.setText ("");
	}
	
	public static void main(String[] args) {
		new App();
	}
}
